package com.rfid.afd.model;

import com.rfid.config.Constant;
import com.rfid.db.bean.Operation;
import com.rfid.db.bean.Rfid;
import com.rfid.util.DBManager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Objects;

public class StatisticsModel {


    public List<Rfid> queryOperateRfids(String name, int type, String start, String end) {
        if(type == 4) {
            return DBManager.queryOperationRfids(name,start,end);
        }
        return DBManager.queryOperationRfids(name,type,start,end);
    }

    public List<Operation> queryOperation(String name, int type, String start, String end) {
        if(type == 4) {//错架
            return DBManager.queryOperation(name,start,end);
        }
        return DBManager.queryOperation(name,type,start,end);
    }

    public void export(List<Statistics> statistics,ExportListener listener) {
        String path = Constant.EXPORT_DIR + "/export.txt";
        File file = new File(path);
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            int i = 0;
            for(Statistics statistics1 : statistics) {
                String text = generateText(statistics1);
                bw.write(text);
                bw.newLine();
                i ++;
                if(listener != null) {
                    listener.onExport(i/(float)statistics.size(),statistics1);
                }
            }
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String generateText(Statistics statistics) {
        Rfid rfid = statistics.getRfid();
        StringBuffer sb = new StringBuffer();
        sb.append(notNullString(rfid.getBarcode()));
        sb.append(",");
        sb.append(notNullString(rfid.getUid()));
        sb.append(",");
        sb.append(notNullString(rfid.getBookName()));
        sb.append(",");
        sb.append(notNullString(rfid.getIsbn()));
        sb.append(",");
        sb.append(notNullString(rfid.getRindex()));
        sb.append(",");
        sb.append(notNullString(rfid.getShelf()));
        sb.append(",");
        sb.append(notNullString(rfid.getSite()));
        sb.append(",");
        sb.append(statistics.getDate());
        return sb.toString();
    }

    private String notNullString(String txt) {
        if(txt == null) {
            return "";
        }
        return txt;
    }

    public static class Statistics {
        String date;
        Rfid rfid;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public Rfid getRfid() {
            return rfid;
        }

        public void setRfid(Rfid rfid) {
            this.rfid = rfid;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Statistics that = (Statistics) o;
            return Objects.equals(rfid, that.rfid);
        }

        @Override
        public int hashCode() {
            return Objects.hash(rfid);
        }
    }

    public interface ExportListener {

        public void onExport(float rate,Statistics statistics);
    }
}
